function [stable_teach_propose stable_school_propose] = func_CF_stable(IJipref_sq_cf,IJipref_choice_sq_cf, IJipref_sq,IJjrank,thetahat, IJVAm1_sq, IJtype1num,   IJVAm2_sq, IJtype2num, IJijVAma_sq,IJclassSize_sq,IJitidx,IJjtidx, i_util_iprop, i_util_jprop )
%Takes preferences and solves for stable allocations and output
%Also computes payments as the gap between new and old preferences plus a
%lump sum adjustment so that the worst off teacher is weakly better off

     % build new preferences
    
       % use the restricted choice utility for the rank
    [B1, IJirank_cf]=sort(IJipref_choice_sq_cf,2,'descend');  % this is sorted in descending order; so the first column in I is the most preferred of the teacher in the relevant row 
    
    % run DA twice (teacher and school propose
    
   [iass_jprop]= galeshapley_gender_unbalanced(IJjrank,IJirank_cf);  % school propose
             
   [jass_iprop]= galeshapley_gender_unbalanced(IJirank_cf,IJjrank);  % teach propose
   
   
   imax=max(IJitidx);
   jmax=max(IJjtidx);
    
   % figure out iass_iprop
   istuff= [ (1:length(jass_iprop))' jass_iprop];
   istuff2=sortrows(istuff,2);
      
   iass_iprop=[ zeros(imax,1)];
   for ii=1:imax
       for i=1:jmax
         if (istuff2(i,2)==ii)
            iass_iprop(ii,1)=istuff2(i,1);
         end
       end
   end     
    
    
   % solve for teacher's utility in the match; do so both with and without
   % the policy 
   % use the utility that doesn't restrict choice
   
    for i=1:min(max(IJitidx),max(IJjtidx))
        i_util_iprop_cf(i,1)= IJipref_sq_cf(i,iass_iprop(i,1));
        i_util_iprop_cf_nopolicy(i,1)= IJipref_sq(i,iass_iprop(i,1));
   end
   
   for i=1:min(max(IJitidx),max(IJjtidx))
        i_util_jprop_cf(i,1)= IJipref_sq_cf(i,iass_jprop(i,1));
        i_util_jprop_cf_nopolicy(i,1)= IJipref_sq(i,iass_jprop(i,1));
   end 
   
   % solve for the lump sum payment that makes every teacher weakly better
   % off
   
   lumpsum_tax_iprop=min(i_util_iprop_cf - i_util_iprop);   % this is teachers pay (if positive) (so the district pays the negative of this)
   lumpsum_tax_jprop=min(i_util_jprop_cf - i_util_jprop);
   
   % solve for spending on the program per teacher
   program_spending_iprop=mean(i_util_iprop_cf - i_util_iprop_cf_nopolicy);
   program_spending_jprop=mean(i_util_jprop_cf - i_util_jprop_cf_nopolicy);
 
   % solve for total spending (per teacher), in units of commute time
   tot_spend_iprop=(program_spending_iprop - lumpsum_tax_iprop)/abs(thetahat(2));
   tot_spend_jprop=(program_spending_jprop - lumpsum_tax_jprop)/abs(thetahat(2));
  

   % solve for student output 
   
    % compute type I average output, and type 2 average output in teach
    % propose
    for i=1:min(max(IJitidx),max(IJjtidx))
          i_type1_tot(i,1)=IJVAm1_sq(i,iass_iprop(i,1)).* IJtype1num(i,iass_iprop(i,1));
          i_type1_stu(i,1)= IJtype1num(i,iass_iprop(i,1));
          
          i_type2_tot(i,1)=IJVAm2_sq(i,iass_iprop(i,1)).* IJtype2num(i,iass_iprop(i,1));
          i_type2_stu(i,1)= IJtype2num(i,iass_iprop(i,1));
    end
        
    stable_teach_propose_type1_cf=sum(i_type1_tot)/sum(i_type1_stu);
    stable_teach_propose_type2_cf=sum(i_type2_tot)/sum(i_type2_stu);
       
     % compute type I average output, and type 2 average output in
     % school propose
     for i=1:min(max(IJitidx),max(IJjtidx))
          i_type1_tot(i,1)=IJVAm1_sq(i,iass_jprop(i,1)).* IJtype1num(i,iass_jprop(i,1));
          i_type1_stu(i,1)= IJtype1num(i,iass_jprop(i,1));
          
          i_type2_tot(i,1)=IJVAm2_sq(i,iass_jprop(i,1)).* IJtype2num(i,iass_jprop(i,1));
          i_type2_stu(i,1)= IJtype2num(i,iass_jprop(i,1));
     end
        
     stable_school_propose_type1_cf=sum(i_type1_tot)/sum(i_type1_stu);
     stable_school_propose_type2_cf=sum(i_type2_tot)/sum(i_type2_stu);  

  
   
   % compute per student output in each assignment 
   % loop over teachers b/c all the teachers are assigned whereas the
   % classes aren't
    
    for i=1:min(max(IJitidx),max(IJjtidx))
          i_output_iprop(i,1)=IJijVAma_sq(i,iass_iprop(i,1)).*IJclassSize_sq(i,iass_iprop(i,1));
          i_num_students(i,1)=IJclassSize_sq(i,iass_iprop(i,1));
    end
  
    stable_teach_propose_achiev_cf=sum(i_output_iprop)/sum(i_num_students);
   
    for i=1:min(max(IJitidx),max(IJjtidx))
          i_output_jprop(i,1)=IJijVAma_sq(i,iass_jprop(i,1)).*IJclassSize_sq(i,iass_jprop(i,1));
          i_num_students(i,1)=IJclassSize_sq(i,iass_jprop(i,1));
    end
    
   stable_school_propose_achiev_cf=sum(i_output_jprop)/sum(i_num_students);   
   
   
    stable_teach_propose=[tot_spend_iprop stable_teach_propose_achiev_cf stable_teach_propose_type1_cf stable_teach_propose_type2_cf];
    stable_school_propose=[tot_spend_iprop stable_school_propose_achiev_cf stable_school_propose_type1_cf stable_school_propose_type2_cf];



end

